﻿using AutoCommunication.Enums;
using AutoCommunication.Helper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AutoCommunication.Base
{
    public abstract class DeviceBase:IDevice
    {
        public event Action<BaseVariable, AlarmEventArgs> AlarmEvent;
        public event Action<IDevice> ConnectedEvent;
        public event Action<BaseVariable> DataChangeEvent;
        public event Action<BaseVariable> CycleReadEvent;
        public event Action<IDevice> DisConnectEvent;

        public int BufferSize { get; set; }
        public bool Connected { get; set; }
        public TimeSpan CycleTime { get; set; }
        public string DevDescription { get; set; }
        public string DevName { get; set; }
        public string FullName { get; set; }
        public EndianFormat Format { get; set; }
        public bool IsActive { get; set; }
        public bool IsFirstConnect { get; set; }
        public bool IsRuning { get; set; }
        public int MaxErrorCountIsDisconnect { get; set; }
        public int MaxReadBits { get; set; }
        public int ReConnectWaitTime { get; set; }
        public int Timeout { get; set; }
        public List<GroupBase> Groups { get; set; }
        public List<Variable> Variables { get; set; }

        public Result Init1()
        {
            return Init2();
        }

        public abstract Result Init2();
        public abstract Result Connect();
        public abstract Result DisConnect();
        public abstract Result Start();
        public abstract Result Stop();
        public abstract Result Pause();
        public abstract Task<Result> WriteAsync(string name, double value);
        public abstract Result SaveJson(string path = "setting.json");

        public abstract CancellationTokenSource CancleToken { get; set; }
    }
}
